﻿namespace EasyCMS.Common.Components
{
    using System;
    using System.Configuration;

    public class Application
    {
        #region Configuration
        /// <summary>
        /// 与应用程序相关的配置信息
        /// </summary>
        public System.Configuration.Configuration Configuration { get; private set; }
        #endregion

        #region 构造与初始化
        /// <summary>
        /// 静态构造
        /// </summary>
        static Application()
        {
            //添加系统重启事件
            //Runtime.SystemReset += new Framework.Events.SystemResetHandler(Reset);
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <returns></returns>
        internal static Application Instance()
        {
            if (application == null)
            {
                application = new Application();
            }
            return application;
        }
        private static Application application;//app单例
        /// <summary>
        /// 私有的实例构造
        /// </summary>
        private Application()
        {
            //读出配置文件
            string ApplicationSettingFilePath = Runtime.ConfigPath + "\\EasyCMS.config";
            if (!System.IO.File.Exists(ApplicationSettingFilePath))
            {
                throw new Exception("Pismire ApplicationSettingFile Not Find : " + ApplicationSettingFilePath);
            }
            ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
            filemap.ExeConfigFilename = ApplicationSettingFilePath;
            Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
        }
        /// <summary>
        /// 重启单例
        /// </summary>
        internal static void Reset()
        {
            application = null;
        }
        #endregion
    }
}
